package com.k.kc.util;

import com.google.common.base.CaseFormat;
import com.google.common.base.Converter;

/**
 * @author k
 */
public class StringFomats {

    public static void test(String lowerUndersorce){
        // 驼峰转下划线, userName -> user_name
        Converter<String, String> converter = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.LOWER_UNDERSCORE);
        // 输出: user_name
        System.out.println(converter.convert("userName"));
        System.out.println();

        // 驼峰转连接符, userName -> user-name
        converter = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.LOWER_HYPHEN);
        // 输出: user-name
        System.out.println(converter.convert("userName"));
        System.out.println();

        // 驼峰转首字符大写驼峰, userName -> UserName
        converter = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_CAMEL);
        // 输出: UserName
        System.out.println(converter.convert("userName"));
        System.out.println();

        // 驼峰转大写下划线, userName -> USER_NAME
        converter = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_UNDERSCORE);
        // 输出: USER_NAME
        System.out.println(converter.convert("userName"));
        System.out.println();

        System.out.println("===============================");
        String a1 = CaseFormat.LOWER_UNDERSCORE.converterTo(CaseFormat.LOWER_CAMEL).convert("user_na_me");
        System.out.println(a1);
    }

    /**
     * userName -> user_name
     * @return string
     */
    public static String lowerCamelToLowerUnderscore(String str){
        return CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.LOWER_UNDERSCORE).convert(str);
    }
    public static String lowerUnderscoreToLowerCamel(String str){
        return CaseFormat.LOWER_UNDERSCORE.converterTo(CaseFormat.LOWER_CAMEL).convert(str);
    }

    public static void main(String[] args) {
        String str = "user_name";
        System.out.println("下划线字符串:"+str);
        String strCov = lowerUnderscoreToLowerCamel(str);
        System.out.println("下划线字符串 -> 驼峰字符串:"+strCov);
        String strDeCov = lowerCamelToLowerUnderscore(strCov);
        System.out.println("驼峰字符串 -> 下划线字符串:"+strDeCov);
    }
}
